## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
## 
## added eventmanager for ViSa by the ViSa team.
## all the credits go to the makers of the G-Era, visit them at http://forums.civfanatics.com/showthread.php?t=184182
## the Team of ViSa thanks them gratefully!

from CvPythonExtensions import *
import CvUtil
import CvEventManager

import CvScreensInterface
import CvDebugTools
import CvWBPopups
import PyHelpers
import Popup as PyPopup
import CvCameraControls
import CvTopCivs
import sys
import CvWorldBuilderScreen
import CvAdvisorUtils
import CvTechChooser
import random

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

   
# globals
###################################################
class CvTechTreeEditorManager:
	def __init__(self, eventManager):

		eventDefinition = ('ChangeTechImage', self.__eventChangeTechImageApply , self.__eventChangeTechImageBegin)
		eventManager.addCustomEventDefinition(CvUtil.EventChangeTechImage, eventDefinition)
		
		eventDefinition = ('ChangeTechName', self.__eventChangeTechNameApply , self.__eventChangeTechNameBegin)
		eventManager.addCustomEventDefinition(CvUtil.EventChangeTechName, eventDefinition)
		
		eventDefinition = ('AddTech', self.__eventAddTechApply , self.__eventAddTechBegin)
		eventManager.addCustomEventDefinition(CvUtil.EventAddTech, eventDefinition)



	# < Tech Tree Editor Start >
	def __eventChangeTechImageApply(self, playerID, userData, popupReturn):	
		gc.getTechInfo(userData[0]).setButton(str(popupReturn.getEditBoxString(0)))
		CvScreensInterface.getTechTreeEditor().placeTechs(userData[0])
		CvScreensInterface.getTechTreeEditor().updateTechButton()
		return

	def __eventChangeTechImageBegin(self):
		return

	def __eventChangeTechNameApply(self, playerID, userData, popupReturn):	
		gc.getTechInfo(userData[0]).setTextKey(str(popupReturn.getEditBoxString(0)))
		CvScreensInterface.getTechTreeEditor().placeTechs(userData[0])
		CvScreensInterface.getTechTreeEditor().updateTechName()
		return

	def __eventChangeTechNameBegin(self):
		return


	def __eventAddTechApply(self, playerID, userData, popupReturn):	
	
		bSuccessful = true
		szReason = ""

		strTechType = "TECH_" + str(popupReturn.getEditBoxString(0)).replace(" ","_").upper()
		
		if(len(str(popupReturn.getEditBoxString(0))) == 0):
			bSuccessful = false
			szReason = "Invalid tech name"

		if(gc.getInfoTypeForString(strTechType) != -1 and not gc.getTechInfo(gc.getInfoTypeForString(strTechType)).isDeleted()):
			bSuccessful = false
			szReason = "The tech with the name: %s already exists" %(str(popupReturn.getEditBoxString(0)))
			
		if(bSuccessful):
			CvScreensInterface.getTechTreeEditor().addTech(str(popupReturn.getEditBoxString(0)))
			return
		
		popup = PyPopup.PyPopup()
		popup.setBodyString(szReason)
		popup.launch()

		return

	def __eventAddTechBegin(self):
		return

	# < Tech Tree Editor End   >
